<?php
class Cms_model extends CI_Model{
	
	//var $CI;
	
	function Cms_model(){
		parent::__construct();
		
		//$CI =& get_instance();
		//$this->load->library('encrypt');
	}
	
	function Login($userid, $pwd){
		$result = false;		
		$userid = strtolower($userid);
		$sql = "SELECT * FROM qf_user WHERE uid =".$this->db->escape($userid)." AND disabled = 0"; 		
		//log_message('debug', 'test##'.$sql);
		
		//$query = $this->db->query($sql, array($userid)); 
		$query = $this->db->query($sql); 
		foreach ($query->result() as $row)
		{
			//$this->encrypt->encode($pwd);
			$pwdstr = $row->pwd;
			if(!empty($pwdstr)){	
				$pwdstr = $this->encrypt->decode($pwdstr);		
				//$pwdstr = $this->encrypt->decode($pwdstr);
				if(strcasecmp($pwdstr, $pwd) == 0){
					$result = true;
					break;
				}
			}
		}
		return $result;		
	}
	
	function ExistUserID($userid){		
		$sql = "SELECT * FROM qf_user WHERE uid ='$userid'";
		$query = $this->db->query($sql); 
		if ($query->num_rows()> 0)
			return true;
		else
			return false;		
	}
	
	function hasChildColumnsBySiteid($siteid)
	{
		$sql = "SELECT count(*) as num FROM qf_column where disabled=0 and sid='$siteid' "; 
		$query = $this->db->query($sql); 
		if ($query->num_rows() > 0){
			$row = $query->row(); 
			if( $row->num == 0 )
				return false;
			else
				return true;
		}
	}
	
	function hasChildColumns($cid)
	{
		$sql = "SELECT count(*) as num FROM qf_column where disabled=0 and parentid='$cid' "; 
		$query = $this->db->query($sql); 
		if ($query->num_rows() > 0){
			$row = $query->row(); 
			if( $row->num == 0 )
				return false;
			else
				return true;
		}
	}
	
	function GetJsonData($parentID)
	{
		/** res = '[' 
			res += '{ "title": "Node 1", "key": "k1", "isLazy": true },' 
			res += '{ "title": "Node 2", "key": "k2", "isLazy": true },' 
			res += '{ "title": "Node 3", "key": "k3", "isLazy": true }' # no trailing "," at the last line
			res += ']'
		*/
		
		try {		
			$col="";
			$result="";
			if($parentID == "")
			{
				//site auth filter
				$uid = $_SESSION['qf_userid'];
				$usersites = $this->GetUserSites($uid);
				if($uid != 'admin')
					if(!is_array($usersites) )
						echo "";
				
				$siteString = '';
				
				$sql = "SELECT * FROM qf_site where disabled=0 order by sortno,sname ";
				$query = $this->db->query($sql); 
				foreach ($query->result() as $row)
				{
					//filter
					if($uid != 'admin'){
						//$sid = $row->sid;
						if( !in_array($row->sid, $usersites))
							continue;
					}
					
					//data
					$siteString .= ",{ \"title\": \"".$row->sname."\"";
					$siteString .= ",\"key\":\"".$row->sid."\"";
					$siteString .= ",\"url\":\"/cms/columns/".$row->sid."\"";
					$siteString .= ",\"isFolder\":\"true\" ";
					$siteString .= ",\"icon\":\"server.png\" ";
					
					if($this->hasChildColumnsBySiteid($row->sid))
						$siteString .=",\"isLazy\": \"true\" ";
						
					$siteString .= "}";
				}
				if(!empty($siteString))
					$siteString = "[".substr($siteString,1)."]";
					
				$result .= "[{\"title\": \"常规\", \"key\": \"setting\", \"isFolder\": true, \"expand\":true, \"children\": [";
				//$result .= "{\"title\": \"我的文档\", \"key\": \"chgpwd\", \"url\": \"/cms/users\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"mydoc.png\" },";
				//$result .= "{\"title\": \"文档审核\", \"key\": \"chgpwd\", \"url\": \"/cms/roles\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"checkdoc.png\" },";
				$result .= "{\"title\": \"文档查询\", \"key\": \"chgpwd\", \"url\": \"/cms/searchart\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"search.png\"  },";
				$result .= "{\"title\": \"修改密码\", \"key\": \"chgpwd\", \"url\": \"/cms/changepwd\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"lock.png\"  }";
				$result .= "]}";
				
				$result .= ",{ \"title\": \"网站管理\", \"key\": \"f_site\", \"url\": \"/cms/sites\", \"isFolder\": true, \"isLazy\": true , \"expand\":true ";
				if(!empty($siteString))
					$result .= ",\"children\": ".$siteString ;
				$result .="}";
				$result .= ",{\"title\": \"系统设置\", \"key\": \"setting\", \"isFolder\": true, \"expand\":true, \"children\": [";
				$result .= "{\"title\": \"用户\", \"key\": \"chgpwd\", \"url\": \"/cms/users\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"user.png\" },";
				$result .= "{\"title\": \"角色\", \"key\": \"chgpwd\", \"url\": \"/cms/roles\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"users.png\" },";
				$result .= "{\"title\": \"模板\", \"key\": \"chgpwd\", \"url\": \"/cms/templates\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"tpl.png\"  },";
				$result .= "{\"title\": \"生成\", \"key\": \"chgpwd\", \"url\": \"/cms/genhtml\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"tplgo.png\"  }";
				
				//$result .= "{\"title\": \"数据\", \"key\": \"chgpwd\", \"url\": \"changepwd.aspx\",\"isFolder\": true, \"isLazy\": false, \"icon\": \"database.png\" }";
				$result .= "]}";
				$result .= "]";
			}
			else 
			{
				if(substr($parentID,0,2) == 's_')
				{
					$sql = "SELECT * FROM qf_column where disabled=0 and sid='$parentID' and parentid='' order by sortno ";}
				else
					$sql = "SELECT * FROM qf_column where disabled=0 and parentid='$parentID' order by sortno ";
				
				$query = $this->db->query($sql); 
				foreach ($query->result_array() as $row)
				{
					$result .= ",{ \"title\": \"".$row['cname']."\"";
					$result .= ",\"key\":\"".$row['cid']."\"";
					$result .= ",\"url\":\"/cms/articles/".$row['cid']."\"";
					$result .= ",\"isFolder\":\"true\" ";
					$result .= ",\"icon\":\"col.gif\" ";
					
					if($this->hasChildColumns($row['cid']))
						$result .=",\"isLazy\": \"true\" ";
						
					$result .= "}";
				}
				if(!empty($result))
					$result = "[".substr($result,1)."]";
			}
			return $result;
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return "";
		}
	}
	
	function GetUserSites($uid){
		try{		
			$r = array();
			$sql = "select distinct r.sid from qf_role r, qf_userrole ur where ur.uid='$uid' and r.disabled=0 and ur.rid=r.roleid";
			$query = $this->db->query($sql); 
			//return $query->result_array();
			foreach ($query->result() as $row) {
			 	array_push($r, $row->sid);
			}
			return  $r;
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return null;
		}
	}
	
	function ListUserSites($uid){
		try{		
			$r = array();
			$usersites = $this->GetUserSites($uid);
			
			$sql = "SELECT * FROM qf_site where disabled=0 order by sortno,sname ";
			$query = $this->db->query($sql); 
			foreach ($query->result() as $row){
				//filter
				if($uid != 'admin'){
					if( !in_array($row->sid, $usersites))
						continue;
				}
				
				$r[$row->sid] = $row->sname;
			}
			return  $r;
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return null;
		}
	}
	
	function GetUserName($userid){
		try{
			$result = '';		
			$sql = "SELECT * FROM qf_user WHERE uid ='$userid'";
			$query = $this->db->query($sql); 
			if ($query->num_rows() > 0){
				$row = $query->row();
				$result = $row->uname;
			}
			return $result;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return '';
		}
	}
	
	function ListSiteByUserID($userid){
		try{
			$result = array();
			$sql = "SELECT r.sid FROM qf_userrole ur, qf_role r where ur.uid='$userid' and r.roleid=ur.rid";
			$query = $this->db->query($sql); 
			foreach ($query->result() as $row){
				array_push($result, $row->sid);
			}
			return $result;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return '';
		}
	}
	
	function ExistUserRole($userid,$roleid){		
		$sql = "SELECT * FROM qf_userrole WHERE uid ='$userid' and rid='$roleid'";
		$query = $this->db->query($sql); 
		if ($query->num_rows()> 0)
			return true;
		else
			return false;		
	}
	
	function GetRoleName($rid){
		try{
			$result = '';		
			$sql = "SELECT * FROM qf_role WHERE roleid ='$rid'";
			$query = $this->db->query($sql); 
			if ($query->num_rows() > 0){
				$row = $query->row();
				$result = $row->rolename;
			}
			return $result;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return '';
		}
	}
	
	//得到用户的权限级别
	function GetRoleLevel($uid){
		try{
			if($uid == 'admin')
				return 3;
				
			$result = 0;		
			$sql = "select max(r.rolelevel) as rolelevel from qf_role r, qf_userrole ur where ur.uid='$uid' and r.disabled=0 and ur.rid=r.roleid;";
			$query = $this->db->query($sql); 
			if ($query->num_rows() > 0){
				$row = $query->row();
				$result = $row->rolelevel;
			}
			return $result;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return 0;
		}
		
	}
	
	function GetSiteIDByColumnID($cid){
		try{
			$result = '';		
			$sql = "SELECT sid FROM qf_column WHERE cid ='$cid'";
			$query = $this->db->query($sql); 
			if ($query->num_rows() > 0){
				$row = $query->row();
				$result = $row->sid;
			}
			return $result;		
		}
		catch (Exception $ex){
			log_message('error',$ex->getMessage());
			return '';
		}
	}
	
	function GetPagerHtml($CurPage, $PageCount){
		$output = "<div class='gridPager'>";
		if ($PageCount > 0){
			$output .= "第";
			
			$LinkNum = 10;
			$PageNumX = 1;
			$CurPageX = 1;
			$PageNumX = ceil( $PageCount / $LinkNum );
			$CurPageX = ceil( $CurPage / $LinkNum);

			if ($CurPageX > 1)
				$output.="<a href=\"javascript:goPage(".($CurPageX - 1) * $LinkNum.")\" ><<</a>";
			
			for ($j = ($CurPageX - 1) * $LinkNum + 1; $j <= $PageCount && $j <= $CurPageX * $LinkNum; $j++)
			{
				if ($j == $CurPage)
					$output.="<a href=\"#\" class=\"curPage\">".$j."</a>";
				else
					$output.="<a href=\"javascript:goPage('$j')\" >".$j."</a>";
			}
			if ($CurPageX < $PageNumX)
				$output.="<a href=\"javascript:goPage(".($CurPageX * $LinkNum + 1).")\" >>></a>";

			$output.= "页,共".$PageCount."页";
		}
		$output.= "</div>";
		return $output;
	}
	
	function GetPagerHtmlForColumnTpl($ColumnID, $CurPage, $PageCount){
		$output = "<div class='gridPager'>";
		if ($PageCount > 0){
			$output .= "第";
			
			$LinkNum = 10;
			$PageNumX = 1;
			$CurPageX = 1;
			$PageNumX = ceil( $PageCount / $LinkNum );
			$CurPageX = ceil( $CurPage / $LinkNum);

			if ($CurPageX > 1)
				$output.="<a href=\"javascript:goPage('$ColumnID',".($CurPageX - 1) * $LinkNum.")\" ><<</a>";
			
			for ($j = ($CurPageX - 1) * $LinkNum + 1; $j <= $PageCount && $j <= $CurPageX * $LinkNum; $j++)
			{
				if ($j == $CurPage)
					$output.="<a href=\"#\" class=\"curPage\">".$j."</a>";
				else
					$output.="<a href=\"javascript:goPage('$ColumnID','$j')\" >".$j."</a>";
			}
			if ($CurPageX < $PageNumX)
				$output.="<a href=\"javascript:goPage('$ColumnID',".($CurPageX * $LinkNum + 1).")\" >>></a>";

			$output.= "页,共".$PageCount."页";
		}
		$output.= "</div>";
		return $output;
	}
}